﻿//Write a program that shows the binary representation of given 16-bit signed integer number (the C# type short).


using System;
using System.Text;

public class BinaryRepresentationOfGivenSignedNumber
{
    public static void Main()
    {
        Console.Write("Enter decimal number to convert Num = ");
        int number = int.Parse(Console.ReadLine());        
        byte[] result = ConvertFromNegativeToBinary(number);
        Console.Write("Converted number to number in base {0} is ");
        Print(result);
        Console.WriteLine();
    }

    public static byte[] ConvertFromNegativeToBinary(int number, int divisor = 2)
    {
        byte[] array;
        if (number < 0)
        {
            number = Math.Abs(++number);
            array = new byte[16];
            for (int i = array.Length - 1; i >= 0; i--)
            {
                array[i] = (byte)(number % divisor == 0 ? 1 : 0);
                number /= divisor;
            }
        }
        else
        {
            array = new byte[(int)(Math.Log(number, divisor) + 1)];
            for (int i = array.Length - 1; i >= 0; i--)
            {
                array[i] = (byte)(number % divisor == 0 ? 1 : 0);
                number /= divisor;
            }
        }
        return array;
    }

    public static void Print(byte[] array)
    {
        foreach (var item in array)
        {
            if (item > 9)
            {
                Console.Write((char)(item + 55));
            }
            else
            {
                Console.Write(item);
            }
        }
    }
}